<?php
namespace Home\Controller;
class LabelController extends CommonController {

    public function _initialize()
    {
        $this->Is_login();        //检测用户登录信息,如果错误直接返回错误,终止程序
        $this->Is_power("system");        //检测用户权限,如果错误直接返回错误,终止程序
        $this->assign('heardadminData',session('heardadminData'));
    }

    //标签列表
    public function label_list()
    {
        $db_label = M("label");
        $labellist = $db_label->select();
        // p($labellist);exit();
        $this->assign('labellist',$labellist);
        $this->display();

    }
    //创建标签
    public function label_add()
    {
        $this->display();
    }
    public function add_label()
    {
        $label_name = I('post.label_name');
        if (empty($label_name)) {
            $this->error('参数错误',"",1);
        }
        $db_label = D("label");
        if (!$db_label->create()){//自动验证
            $this->error('参数错误',"",1);
        }else{
            $data = $db_label->create(); // 保存生成的数据对象
            $data['label_name'] = $label_name;
            // p($data);exit();
            $result = $db_label->add($data);
            $cid['label_id'] = "-".(int)$result."-";
            $db_label->where(array('id'=>$result))->save($cid);
        $this->adminlog("创建分类".$result);
            $this->success('创建成功',"/Home/Label/label_add",1);
        }
    }


    //修改标签
    public function label_edit()
    {
        $id = I('get.id');
        $labeldata = M("label")->where(array("id"=>$id))->find();
        // p($labeldata);exit();
        $this->assign('labeldata',$labeldata);
        $this->display();
    }

    public function edit_label()
    {
        $id = I('post.id');
        $label_name = I('post.label_name');
        if (empty($id)||empty($label_name)) {
            $this->error('参数错误',"",1);
        }
        $label = D("label");
        if (!$label->create()){//自动验证
            $this->error('参数错误',"",1);
        }else{
            $data = $label->create(); // 保存生成的数据对象
            $data['label_name'] = $label_name;
            $label->where(array('id'=>$id))->save($data);//更新数据
        $this->adminlog("修改分类".$id);
            $this->success('修改成功',"/Home/Label/label_list",1);
        }
    }
    /*public function label_list()
    {
        $labellist = C('articles');//读取配置
        $this->assign('labellist',$labellist);
        $this->display();
    }
    public function label_add()
    {
        $this->display();
    }
    public function add_label()
    {
        $label_name = I('post.label_name');
        if (empty($label_name)) {
            $this->error('参数错误',"",1);
        }
        $labellist['articles'] = C('articles');//读取配置
        foreach ($labellist['articles'] as $key => $value) { }
        $keyid = "-".(substr(substr($key,1),0,-1)+1)."-";
        $data[$keyid] = $label_name;
        $labellist['articles'] = array_merge($labellist['articles'],$data);
        $filename = $_SERVER['DOCUMENT_ROOT'] . '/Application/Home/Conf/articles.php';
        file_put_contents($filename, strip_whitespace("<?php\treturn " . var_export($labellist, true) . ";?>"));
        $this->success('创建成功',"/Home/Label/label_add",1);

    }
    public function label_edit()
    {
        $keyid = I('get.key');
        $labellist = C('articles');//读取配置
        $labeldata['key'] = $keyid;
        foreach ($labellist as $key => $value) {
            if ($key == $keyid) {
                $labeldata['value'] = $value;
            }
        }
        $this->assign('labeldata',$labeldata);
        $this->display();
    }
    public function edit_label()
    {
        $keyid = I('post.key');
        $label_name = I('post.label_name');
        if (empty($label_name)) {
            $this->error('参数错误',"",1);
        }
        $labellist['articles'] = C('articles');//读取配置
        foreach ($labellist['articles'] as $key => &$value) {
            if ($key == $keyid) {
                $value = $label_name;
            }
        }
        $filename = $_SERVER['DOCUMENT_ROOT'] . '/Application/Home/Conf/articles.php';
        file_put_contents($filename, strip_whitespace("<?php\treturn " . var_export($labellist, true) . ";?>"));
        $this->success('修改成功',"/Home/Label/label_list",1);
    }*/


}